﻿package  
{
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.geom.Point;
	import flash.net.FileReference;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	public class Recorder extends MovieClip
	{
		
		private var _tf :TextField;
		
		private var _mode :int;
		
		// private var _pts :/*Point*/Array;
		// private var _tts :/*Number*/Array;
		
		private var _phase :int;
		
		private var _fr :FileReference;
		
		private var _data  :String;
		private var _data2 :String;
		
		private var _startTime :uint;
		
		/**
		 * 
		 * @param	mode		0: マウスの動き, 1: クリック時の時間、位置、
		 */
		public function Recorder( m :MovieClip, mode :int = 0 ) 
		{
			_mode = mode;
			
			//_pts = [];
			//_tts = [];
			
			_data  = "";
			_data2 = "";
			
			
			
			// Text
			var ft :TextFormat = new TextFormat();
			ft.letterSpacing = 1.5;
			ft.size = 10;
			ft.font = "_sans";
			
			_tf = new TextField();
			_tf.defaultTextFormat = ft;
			_tf.autoSize = "left";
			_tf.selectable = false;
			_tf.textColor = 0xaaaaaa;
			_tf.text = "click";
			addChild( _tf );
			
			m.stage.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown );
			m.stage.addEventListener(KeyboardEvent.KEY_DOWN, _keyDown);
		}
		/* 仮スタート（スタートというより、ステップ進める） */
		public function start ( ) :void
		{
			_mouseDown( null );
		}
		
		private function _save ( ) :void
		{
			_fr = new FileReference();
			
			var ba :ByteArray = new ByteArray();
			
			var date :Date = new Date();
			;
			
			switch ( _mode )
			{
			case 0:
				
				_data  = _data.slice(0, _data.length - 1);
				
				ba.writeMultiByte( _data, "utf-8" );
				_fr.save( ba, "rec_" + date.getTime() + ".txt" );
				break;
			case 1:
				
				_data = _data.slice(0, _data.length - 1);
				_data2 = _data2.slice(0, _data2.length - 1);
				
				
				ba.writeMultiByte( _data, "utf-8" );
				ba.writeMultiByte( "\r\n" + _data2, "utf-8" );
				
				_fr.save( ba, "rec2_" + date.getTime() + ".txt" );
				break;
			}
			
			_tf.text = "click";
			_data  = "";
			_data2 = "";
			_phase = 0;
			
		}
		
		private function _mouseDown ( evt :Event ) :void
		{
			switch ( _mode )
			{
				case 0:	_rec0();	break;
				case 1:	_rec1();	break;
			}
		}
		private function _keyDown ( evt :KeyboardEvent ) :void
		{
			if ( _mode == 1 )
			{
				switch ( evt.keyCode )
				{
				case Keyboard.SPACE:
					
					trace("rec stop");
					
					_save();
					break;
				}
			}
			else
			{
				
			}
		}
		private function _rec0 ( ) :void
		{
			switch ( _phase )
			{
			case 0:
				trace("rec start : move mouse.");
				_tf.text = "rec start : move mouse.";
				
				removeEventListener(Event.ENTER_FRAME, _enterFrame);
				addEventListener(Event.ENTER_FRAME, _enterFrame);
				_phase ++;
				break;
			case 1:
				trace("rec stop");
				
				removeEventListener(Event.ENTER_FRAME, _enterFrame);
				_save();
				break;
			}
			
		}
		private function _rec1 ( ) :void
		{
			switch ( _phase )
			{
			case 0:
				trace("rec start : timer start");
				_tf.text = "rec start & timer start : click";
				
				_startTime = getTimer();
				_phase ++;
			break;
			default:
				trace("rec");
				_tf.text = "rec " + mouseX + "," + mouseY;
				
				_data  += mouseX + "," + mouseY + ",";
				_data2 += (getTimer() - _startTime) + ",";
			break;
			}	
		}
		
		private function _enterFrame ( evt :Event ) :void
		{
			_data += mouseX + "," + mouseY + ",";
		}
		
	}
}